const { exeCommandSpawn, isWindows, packageManager } = require("../utils");
const t = require("../../config/dict");
const ora = require("ora");
const path = require("path");

module.exports = (context) => {
  return new Promise((resolve, reject) => {
    const packageName = context.metadata.projectUpdateDepend;
    const _LOCALSOURCE = context.source;
    /* const command = [
      isWindows ? "npm.cmd" : "npm",
      "install",
      ...(/mfo/.test(packageName)
        ? ["-g", packageName]
        : packageName.split(" ")),
      "--registry",
      _LOCALSOURCE,
    ].join(" "); */
    const spinner = ora(
      [
        t(context.lang, "升级依赖包"),
        ",",
        t(context.lang, "请稍候"),
        " ...",
      ].join(" ")
    );

    spinner.start();
    packageManager()
      .install(
        [
          ...(/mfo/.test(packageName)
            ? ["-g", packageName]
            : packageName.split(" ")),
        ],
        { register: _LOCALSOURCE }
      )
      /* exeCommandSpawn(command, {
      shell: true,
    }) */
      .then(() => {
        spinner.succeed(
          [
            t(context.lang, "升级依赖包"),
            t(context.lang, "完成"),
            t(context.lang, "。"),
          ].join(" ")
        );
        resolve();
      })
      .catch((err) => {
        spinner.fail(
          [
            t(context.lang, "升级依赖包"),
            t(context.lang, "失败"),
            t(context.lang, "。"),
          ].join(" ")
        );
        reject(err);
      });
  });
};
